package com.cocoblue.nextjslearn;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.json.JSONUtil;
import com.cocoblue.nextjslearn.entity.Customer;
import com.cocoblue.nextjslearn.entity.Invoice;
import com.cocoblue.nextjslearn.entity.Revenue;
import com.cocoblue.nextjslearn.entity.SysUser;
import com.cocoblue.nextjslearn.repo.CustomerRepository;
import com.cocoblue.nextjslearn.repo.InvoiceRepository;
import com.cocoblue.nextjslearn.repo.RevenueRepository;
import com.cocoblue.nextjslearn.repo.SysUserRepository;
import jakarta.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Component
public class InitService {

    @Autowired
    private SysUserRepository sysUserRepository;
    @Autowired
    private InvoiceRepository invoiceRepository;
    @Autowired
    private RevenueRepository revenueRepository;
    @Autowired
    private CustomerRepository customerRepository;


    @PostConstruct
    public void initSysUser(){
        sysUserRepository.save(new SysUser().setId(1L).setName("Tom").setEmail("tom@ocbc.com").setPassword("password"));

        //customer
        String customerJsonStr="[\n" +
                "  {\n" +
                "    id: 1,\n" +
                "    name: 'Delba de Oliveira',\n" +
                "    email: 'delba@oliveira.com',\n" +
                "    image_url: '/customers/delba-de-oliveira.png',\n" +
                "  },\n" +
                "  {\n" +
                "    id: 2,\n" +
                "    name: 'Lee Robinson',\n" +
                "    email: 'lee@robinson.com',\n" +
                "    image_url: '/customers/lee-robinson.png',\n" +
                "  },\n" +
                "  {\n" +
                "    id: 3,\n" +
                "    name: 'Hector Simpson',\n" +
                "    email: 'hector@simpson.com',\n" +
                "    image_url: '/customers/hector-simpson.png',\n" +
                "  },\n" +
                "  {\n" +
                "    id: 4,\n" +
                "    name: 'Steven Tey',\n" +
                "    email: 'steven@tey.com',\n" +
                "    image_url: '/customers/steven-tey.png',\n" +
                "  },\n" +
                "  {\n" +
                "    id: 5,\n" +
                "    name: 'Steph Dietz',\n" +
                "    email: 'steph@dietz.com',\n" +
                "    image_url: '/customers/steph-dietz.png',\n" +
                "  },\n" +
                "  {\n" +
                "    id: 6,\n" +
                "    name: 'Michael Novotny',\n" +
                "    email: 'michael@novotny.com',\n" +
                "    image_url: '/customers/michael-novotny.png',\n" +
                "  },\n" +
                "  {\n" +
                "    id: 7,\n" +
                "    name: 'Evil Rabbit',\n" +
                "    email: 'evil@rabbit.com',\n" +
                "    image_url: '/customers/evil-rabbit.png',\n" +
                "  },\n" +
                "  {\n" +
                "    id: 8,\n" +
                "    name: 'Emil Kowalski',\n" +
                "    email: 'emil@kowalski.com',\n" +
                "    image_url: '/customers/emil-kowalski.png',\n" +
                "  },\n" +
                "  {\n" +
                "    id: 9,\n" +
                "    name: 'Amy Burns',\n" +
                "    email: 'amy@burns.com',\n" +
                "    image_url: '/customers/amy-burns.png',\n" +
                "  },\n" +
                "  {\n" +
                "    id: 10,\n" +
                "    name: 'Balazs Orban',\n" +
                "    email: 'balazs@orban.com',\n" +
                "    image_url: '/customers/balazs-orban.png',\n" +
                "  },\n" +
                "]";
        List<Customer> customerList = JSONUtil.parseArray(customerJsonStr).stream().map(e -> {
            return BeanUtil.toBean(e, Customer.class);
        }).collect(Collectors.toList());
        customerRepository.saveAll(customerList);

        //customer
        String invoicesJsonStr="[\n" +
                "  {\n" +
                "    id:\"1\",\n" +
                "    customer_id: 1,\n" +
                "    amount: 15795,\n" +
                "    status: 'pending',\n" +
                "    date: '2022-12-06',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"2\",\n" +
                "    customer_id: 2,\n" +
                "    amount: 20348,\n" +
                "    status: 'pending',\n" +
                "    date: '2022-11-14',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"3\",\n" +
                "    customer_id: 5,\n" +
                "    amount: 3040,\n" +
                "    status: 'paid',\n" +
                "    date: '2022-10-29',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"4\",\n" +
                "    customer_id: 4,\n" +
                "    amount: 44800,\n" +
                "    status: 'paid',\n" +
                "    date: '2023-09-10',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"5\",\n" +
                "    customer_id: 6,\n" +
                "    amount: 34577,\n" +
                "    status: 'pending',\n" +
                "    date: '2023-08-05',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"6\",\n" +
                "    customer_id: 8,\n" +
                "    amount: 54246,\n" +
                "    status: 'pending',\n" +
                "    date: '2023-07-16',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"7\",\n" +
                "    customer_id: 7,\n" +
                "    amount: 666,\n" +
                "    status: 'pending',\n" +
                "    date: '2023-06-27',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"8\",\n" +
                "    customer_id: 4,\n" +
                "    amount: 32545,\n" +
                "    status: 'paid',\n" +
                "    date: '2023-06-09',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"9\",\n" +
                "    customer_id: 5,\n" +
                "    amount: 1250,\n" +
                "    status: 'paid',\n" +
                "    date: '2023-06-17',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"10\",\n" +
                "    customer_id: 6,\n" +
                "    amount: 8546,\n" +
                "    status: 'paid',\n" +
                "    date: '2023-06-07',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"11\",\n" +
                "    customer_id: 2,\n" +
                "    amount: 500,\n" +
                "    status: 'paid',\n" +
                "    date: '2023-08-19',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"12\",\n" +
                "    customer_id: 6,\n" +
                "    amount: 8945,\n" +
                "    status: 'paid',\n" +
                "    date: '2023-06-03',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"13\",\n" +
                "    customer_id: 3,\n" +
                "    amount: 8945,\n" +
                "    status: 'paid',\n" +
                "    date: '2023-06-18',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"14\",\n" +
                "    customer_id: 1,\n" +
                "    amount: 8945,\n" +
                "    status: 'paid',\n" +
                "    date: '2023-10-04',\n" +
                "  },\n" +
                "  {\n" +
                "    id:\"15\",\n" +
                "    customer_id: 3,\n" +
                "    amount: 1000,\n" +
                "    status: 'paid',\n" +
                "    date: '2022-06-05',\n" +
                "  },\n" +
                "]";
        List<Invoice> invoiceList = JSONUtil.parseArray(invoicesJsonStr).stream().map(e -> BeanUtil.toBean(e, Invoice.class)).collect(Collectors.toList());
        invoiceRepository.saveAll(invoiceList);

        //customer
        String revenueStr="[\n" +
                "  { monthStr: 'Jan', revenue: 2000 },\n" +
                "  { monthStr: 'Feb', revenue: 1800 },\n" +
                "  { monthStr: 'Mar', revenue: 2200 },\n" +
                "  { monthStr: 'Apr', revenue: 2500 },\n" +
                "  { monthStr: 'May', revenue: 2300 },\n" +
                "  { monthStr: 'Jun', revenue: 3200 },\n" +
                "  { monthStr: 'Jul', revenue: 3500 },\n" +
                "  { monthStr: 'Aug', revenue: 3700 },\n" +
                "  { monthStr: 'Sep', revenue: 2500 },\n" +
                "  { monthStr: 'Oct', revenue: 2800 },\n" +
                "  { monthStr: 'Nov', revenue: 3000 },\n" +
                "  { monthStr: 'Dec', revenue: 4800 },\n" +
                "]";
        List<Revenue> revenueList = JSONUtil.parseArray(revenueStr).stream().map(e -> BeanUtil.toBean(e, Revenue.class)).collect(Collectors.toList());
        revenueRepository.saveAll(revenueList);
    }

}
